home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dave falkenburg's sprocket / lib / window.cp < prev   
Encoding:
Text File  |  2000-09-28  |  22.8 KB  |  910 lines

  1. /*
  2.     File:        Window.cp
  3.  
  4.     Contains:    Implementation of TWindow, a base class which provides a
  5.                 framework for building way-cool windows which even John
  6.                 Sullivan would be happy with. Floating windows and “smart
  7.                 zooming” algorithms are based on code samples provided by
  8.                 Dean Yu. Tim Craycroft, the guy making the window manager
  9.                 do all this work for you has also been a great help.
  10.  
  11.     Written by: Dave Falkenburg    
  12.  
  13.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  14.  
  15.                 You may incorporate this Apple sample source code into your program(s) without
  16.                 restriction. This Apple sample source code has been provided "AS IS" and the
  17.                 responsibility for its operation is yours. You are not permitted to redistribute
  18.                 this Apple sample source code as "Apple sample source code" after having made
  19.                 changes. If you're going to re-distribute the source, we require that you make
  20.                 it clear in the source that the code was descended from Apple sample source
  21.                 code, but that you've made changes.
  22.  
  23.     Change History (most recent first):
  24.                 8/19/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  25.                 12/6/94        DRF                Rolled in David Den Boer’s fixes. Also add the conditionals for
  26.                                             newest universal headers again.
  27.                 11/23/94    DRF                Bite the bullet just require the latest universal headers
  28.                  11/17/94    DRF                Add casts for CFront & PPCC. Also dealt with the change to
  29.                                             ClipAbove in the latest universal headers.
  30.                  11/12/94    DRF                Added AdjustMenusBeforeMenuSelection.
  31.                 11/8/94        DRF                Add some better menu handling methods.
  32.                 9/27/94        DRF                 AppLib.h is now Sprocket.h
  33.                 9/9/94        DRF                Reorganized headers and removed redundant #includes.
  34.                  9/4/94        DRF                Added DrawJustTheGrowIcon.
  35.                 8/27/94        DRF                In TWindow::Close, call window’s (de)Activate method before
  36.                                             closing so that menus can be properly updated.
  37.     
  38.     To Do:        Make sure invisible windows can be created & managed
  39.                 Handle modal windows as another class of windows
  40.                 Fix activate bugs when showing and hiding windows
  41.                 Window positioning methods (getters and setters)
  42.                 Display Manager support
  43.                 Changes to support AEObject model
  44.  
  45. */
  46. #include "Sprocket.h"
  47. #include "Window.h"
  48.  
  49. #include <Script.h>        //    for GetMBarHeight()
  50. #include <LowMem.h>        //    for LMGetWindowList()
  51.  
  52.  
  53. const short            kFloatingWindowKind        = 1000;
  54. const short            kNormalWindowKind        = 1001;
  55. const WindowPtr     kNoFloatingWindows        = (WindowPtr) -1;
  56.  
  57. const short            kScreenEdgeSlop            = 4;
  58. const short            kSpaceForFinderIcons    = 64;
  59. const short            kMinimumTitleBarHeight    = 21;
  60. const short            kMinimumWindowSize        = 32;
  61.  
  62. static void            HiliteShowHideFloatingWindows(Boolean hiliting,Boolean hiding);
  63.  
  64. static void            FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect, GDHandle * theBestDevice);
  65. static pascal void    CalculateWindowAreaOnDevice(short depth,short deviceFlags,GDHandle targetDevice,long userData);
  66.  
  67.  
  68. struct    CalcWindowAreaDeviceLoopUserData
  69.     {
  70.     GDHandle    fScreenWithLargestPartOfWindow;
  71.     long        fLargestArea;
  72.     Rect        fWindowBounds;
  73.     };
  74.  
  75.  
  76.  
  77.  
  78.  
  79. TWindow::TWindow()
  80.     {
  81.     }
  82.  
  83.  
  84. TWindow::~TWindow()
  85.     {
  86.     }
  87.  
  88.  
  89. void
  90. TWindow::CreateWindow(WindowType typeOfWindowToCreate /* = kNormalWindow */)
  91.     {
  92.     WindowPtr    behindWindow,oldFrontMostWindow;
  93.     
  94.     if (typeOfWindowToCreate == kModalWindow)
  95.         {
  96.         DebugStr((StringPtr) "\pModal windows aren’t supported yet");
  97.         fWindowType = kFloatingWindow;
  98.         return;
  99.         }
  100.     else if (typeOfWindowToCreate == kFloatingWindow)
  101.         {
  102.         behindWindow = (WindowPtr) -1;
  103.         oldFrontMostWindow = FrontWindow();
  104.  
  105.         fWindowType = kFloatingWindow;
  106.         }
  107.     else if (typeOfWindowToCreate == kNormalWindow)
  108.         {
  109.         behindWindow = LastFloatingWindow();
  110.  
  111.         fWindowType = kNormalWindow;
  112.         
  113.         if (behindWindow == kNoFloatingWindows)
  114.             oldFrontMostWindow = MyFrontNonFloatingWindow();    // •••
  115.         else
  116.             oldFrontMostWindow = (WindowPtr) ((WindowPeek) behindWindow)->nextWindow;
  117.         }
  118.  
  119.     fWindow = this->MakeNewWindow(behindWindow);
  120.     fIsVisible = ((WindowPeek) fWindow)->visible;
  121.  
  122.     if (fWindow)
  123.         {
  124.         SetWRefCon(fWindow,(long) this);
  125.  
  126.         if (typeOfWindowToCreate == kModalWindow)
  127.             {
  128.             DebugStr((StringPtr) "\pCan’t create Modal windows yet");
  129.             }
  130.         else if (typeOfWindowToCreate == kFloatingWindow)
  131.             {
  132.             ((WindowPeek) fWindow)->windowKind = kFloatingWindowKind;
  133.             
  134.             //    make sure the other window stays hilited
  135.             if (oldFrontMostWindow)
  136.                 HiliteAndActivateWindow(oldFrontMostWindow,true);
  137.             }
  138.         else if (typeOfWindowToCreate == kNormalWindow)
  139.             {
  140.             ((WindowPeek) fWindow)->windowKind = kNormalWindowKind;
  141.  
  142.             //    unhighlight the old front window
  143.             if (oldFrontMostWindow)
  144.                 HiliteAndActivateWindow(oldFrontMostWindow,false);
  145.  
  146.             //    hilite the new window…
  147.             HiliteAndActivateWindow(fWindow,true);
  148.             }
  149.         }
  150.     }
  151.  
  152.  
  153. void
  154. TWindow::AdjustCursor(EventRecord * /* anEvent */)
  155.     {
  156.     }
  157.  
  158. void
  159. TWindow::Idle(EventRecord * /* anEvent */)
  160.     {
  161.     }
  162.     
  163. void
  164. TWindow::Activate(Boolean /* activating */)
  165.     {
  166.     }
  167.     
  168. void
  169. TWindow::Draw(void)
  170.     {
  171.     }
  172.     
  173. void
  174. TWindow::Click(EventRecord * /* anEvent */)
  175.     {
  176.     }
  177.     
  178. void
  179. TWindow::KeyDown(EventRecord * /* anEvent */)
  180.     {
  181.     }
  182.  
  183.  
  184. void
  185. TWindow::Select(void)
  186.     {
  187.     WindowPtr    currentFrontWindow;
  188.     
  189.     if (fWindowType == kFloatingWindow)
  190.         currentFrontWindow = FrontWindow();
  191.     else if (fWindowType == kNormalWindow)
  192.         currentFrontWindow = MyFrontNonFloatingWindow();
  193.     else
  194.         {
  195.         }
  196.  
  197.     if (currentFrontWindow != fWindow)
  198.         {
  199.         if (fWindowType == kFloatingWindow)
  200.             BringToFront(fWindow);
  201.         else
  202.             {
  203.             WindowPtr    lastFloater = LastFloatingWindow();
  204.  
  205.             // Deactivate the window currently in front.
  206.  
  207.             HiliteAndActivateWindow(currentFrontWindow,false);
  208.  
  209.             if (lastFloater == kNoFloatingWindows)
  210.                 {
  211.                 //    If there are no floating windows,
  212.                 //    just call SelectWindow like the good ol’ days
  213.  
  214.                 SelectWindow(fWindow);
  215.                 }
  216.             else
  217.                 {
  218.                 // Bring it behind the last floating window and activate it.
  219.                 // Note that Inside Mac 1 states that you need to call PaintOne() and CalcVis() on a
  220.                 // window if you are using SendBehind() to bring it closer to the front.  With System 7,
  221.                 // this is no longer necessary.
  222.  
  223.                 SendBehind(fWindow,lastFloater);
  224.                 }
  225.  
  226.             //    Hilite the new front window
  227.  
  228.             HiliteAndActivateWindow(fWindow,true);
  229.             }
  230.         }
  231.     }
  232.  
  233.  
  234. void
  235. TWindow::Drag(Point startPoint)
  236.     {
  237.     GrafPtr        savePort;
  238.     KeyMap        theKeyMap;
  239.     Boolean        commandKeyDown = false;
  240.     RgnHandle    draggingRegion;
  241.     long        dragResult;
  242.     
  243.     if (WaitMouseUp())        //    de-bounce?
  244.         {
  245.         // Set up the Window Manager port.
  246.     
  247.         GetPort(&savePort);
  248.         SetPort(gWindowManagerPort);
  249.         SetClip(GetGrayRgn());
  250.  
  251.         // Check to see if the command key is down.
  252.     
  253.         GetKeys(theKeyMap);
  254.         commandKeyDown = ((theKeyMap[1] & 0x8000) != 0);
  255.         
  256.         if (commandKeyDown)
  257.             {
  258.             //    We’re not going to change window ordering,
  259.             //    so make sure that we don’t drag in front of
  260.             //    other windows which may be in front of ours.
  261.             ClipAbove(fWindow);
  262.             }
  263.         else if (fWindowType != kFloatingWindow)
  264.             {
  265.             //    We’re dragging a normal window, so make sure
  266.             //    that we don’t drag in front of any floating
  267.             //    windows.
  268.  
  269.             ClipAbove(MyFrontNonFloatingWindow());
  270.             }
  271.         
  272.         //    Drag an outline of the window around the desktop.
  273.         //    NOTE: DragGrayRgn destroys the region passed in, so make a copy
  274.  
  275.         draggingRegion = NewRgn();
  276.         CopyRgn(((WindowPeek)fWindow)->strucRgn,draggingRegion);
  277.         dragResult = DragGrayRgn(draggingRegion, startPoint, &gDeskRectangle, &gDeskRectangle, noConstraint, nil);
  278.         DisposeRgn(draggingRegion);
  279.  
  280.     
  281.         SetPort(savePort);    //    Get back to old port
  282.  
  283.         if ((dragResult != 0) && (dragResult != 0x80008000))
  284.             {
  285.             this->Nudge((short) (dragResult & 0xFFFF),(short) (dragResult >> 16));
  286.             }
  287.         }
  288.  
  289.     if (!commandKeyDown)
  290.         Select();
  291.     }
  292.  
  293. void
  294. TWindow::Nudge(short horizontalDistance, short verticalDistance)
  295.     {
  296.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  297.     short        newHorizontalPosition,newVerticalPosition;
  298.     
  299.     newHorizontalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.left + horizontalDistance;
  300.     newVerticalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.top + verticalDistance;
  301.  
  302.     MoveWindow(fWindow,newHorizontalPosition,newVerticalPosition,false);
  303.     }
  304.  
  305. void
  306. TWindow::Grow(Point startPoint)
  307.     {
  308.     GrafPtr    oldPort;
  309.     long    newSize;
  310.     Rect    oldWindowRect,resizeLimits;
  311.     
  312.     GetPort(&oldPort);
  313.     
  314.     GetWindowSizeLimits(&resizeLimits);
  315.     newSize = GrowWindow(fWindow,startPoint,&resizeLimits);
  316.     if (newSize)
  317.         {
  318.         oldWindowRect = fWindow->portRect;
  319.         SizeWindow(fWindow,(short) newSize,(short) (newSize >> 16),true);
  320.         SetPort(fWindow);
  321.         this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  322.         }
  323.     
  324.     SetPort(oldPort);
  325.     }
  326.  
  327.  
  328. void
  329. TWindow::Zoom(short zoomState)
  330.     {
  331.     GrafPtr        oldPort;
  332.     FontInfo    systemFontInfo;
  333.     short        titleBarHeight;
  334.     Rect        bestScreenRect,perfectWindowRect,scratchRect;
  335.     short        amountOffscreen;
  336.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  337.     GDHandle    bestDevice;
  338.     
  339.     GetPort(&oldPort);
  340.  
  341.     //    Figure out the height of the title bar so we can properly position
  342.     //    a window. The algorithm is stolen from the System 7.x 'WDEF' (0)
  343.     //
  344.     //    This probably isn’t the best thing to do: A better way might be 
  345.     //    to diff the structure and content region rectangles?
  346.  
  347.     SetPort(gWindowManagerPort);
  348.     GetFontInfo(&systemFontInfo);
  349.     titleBarHeight = (short) (systemFontInfo.ascent + systemFontInfo.descent + 4);
  350.     if ((titleBarHeight % 2) == 1)
  351.         titleBarHeight--;
  352.     if (titleBarHeight < kMinimumTitleBarHeight)
  353.         titleBarHeight = kMinimumTitleBarHeight;
  354.  
  355.  
  356.     //    Only do the voodoo magic if we are really “zooming” the window.
  357.  
  358.     if (zoomState == inZoomOut)
  359.         {
  360.         FindScreenRectWithLargestPartOfWindow(fWindow,&bestScreenRect,&bestDevice);
  361.         bestScreenRect.top += titleBarHeight;
  362.  
  363.         this->GetPerfectWindowSize(&perfectWindowRect);
  364.         OffsetRect(&perfectWindowRect,-perfectWindowRect.left,-perfectWindowRect.top);
  365.  
  366.         //    Take the zero-pined perfect window size and move it to
  367.         //    the top left of the    window’s content region.
  368.  
  369.         OffsetRect(&perfectWindowRect,(**windowAsWindowPeek->contRgn).rgnBBox.left,
  370.                                       (**windowAsWindowPeek->contRgn).rgnBBox.top);
  371.  
  372.         
  373.         //    Does perfectWindowRect fit completely on the best screen?
  374.         
  375.         SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  376.         if (!EqualRect(&perfectWindowRect, &scratchRect))
  377.             {
  378.             //    SectRect sez perfectWindowRect doesn’t completely fit
  379.             //    on the screen, so bump the window so that more of it fits.
  380.  
  381.             //    Make sure that the left edge of perfectWindowRect is forced
  382.             //    onto the best screen.  This is in case we are bumping
  383.             //    the window to the right.
  384.  
  385.             amountOffscreen = bestScreenRect.left - perfectWindowRect.left;
  386.             if (amountOffscreen > 0)
  387.                 {
  388.                 perfectWindowRect.left += amountOffscreen;
  389.                 perfectWindowRect.right += amountOffscreen;
  390.                 }
  391.  
  392.             //    Make sure that the left edge of perfectWindowRect is forced
  393.             //    onto the best screen.  This is in case we are bumping
  394.             //    the window downward to a new screen.
  395.     
  396.             amountOffscreen = bestScreenRect.top - perfectWindowRect.top;
  397.             if (amountOffscreen > 0)
  398.                 {
  399.                 perfectWindowRect.top += amountOffscreen;
  400.                 perfectWindowRect.bottom += amountOffscreen;
  401.                 }
  402.  
  403.             //    If right edge of window falls off the screen,
  404.             //        Move window to the left until the right edge IS on the screen
  405.             //        OR the left edge is at bestScreenRect.left
  406.  
  407.             amountOffscreen = perfectWindowRect.right - bestScreenRect.right;
  408.             if (amountOffscreen > 0)
  409.                 {
  410.                 //    Are we going to push the left edge offscreen? If so, change the
  411.                 //    offset so we move the window all the way over to the left.
  412.                 
  413.                 if ((perfectWindowRect.left - amountOffscreen) < bestScreenRect.left)
  414.                     amountOffscreen = perfectWindowRect.left - bestScreenRect.left;
  415.  
  416.                 perfectWindowRect.left -= amountOffscreen;
  417.                 perfectWindowRect.right -= amountOffscreen;
  418.                 }
  419.  
  420.             //    If bottom edge of window falls off the screen,
  421.             //        Move window to up until the bottom edge IS on the screen
  422.             //        OR the top edge is at bestScreenRect.top
  423.  
  424.             amountOffscreen = perfectWindowRect.bottom - bestScreenRect.bottom;
  425.             if (amountOffscreen > 0)
  426.                 {
  427.                 //    Are we going to push the top edge offscreen? If so, change the
  428.                 //    offset so we move the window just to the top.
  429.                 
  430.                 if ((perfectWindowRect.top - amountOffscreen) < bestScreenRect.top)
  431.                     amountOffscreen = perfectWindowRect.top - bestScreenRect.top;
  432.  
  433.                 perfectWindowRect.top -= amountOffscreen;
  434.                 perfectWindowRect.bottom -= amountOffscreen;
  435.                 }
  436.  
  437.             SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  438.             if (!EqualRect(&perfectWindowRect, &scratchRect))
  439.                 {
  440.                 //    The edges of the window still fall offscreen,
  441.                 //    so make the window smaller until it fits.
  442.                 
  443.                 if (perfectWindowRect.bottom > bestScreenRect.bottom)
  444.                     perfectWindowRect.bottom = bestScreenRect.bottom;
  445.  
  446.                 //    If the right edge is still falling off,
  447.                 //        save space for Finder’s disk icons as well.
  448.  
  449.                 if (perfectWindowRect.right > bestScreenRect.right)
  450.                     {
  451.                     perfectWindowRect.right = bestScreenRect.right;
  452.                     
  453.                     //    If we were on the main screen, leave room for Finder icons, too.
  454.                     
  455.                     if (bestDevice == GetMainDevice())
  456.                         perfectWindowRect.right -= kSpaceForFinderIcons;
  457.                     }
  458.                 }
  459.             }
  460.  
  461.         //    Stash our new rectangle inside of the Window’s dataHandle
  462.         //    so that ZoomWindow does the right thing.
  463.         
  464.         (**((WStateDataHandle) (windowAsWindowPeek->dataHandle))).stdState = perfectWindowRect;
  465.         }
  466.  
  467.     //    HEY YOU! Don’t forget to set the port to the window being zoomed
  468.     //    Why, you ask? Because IM-IV-50 says to; otherwise you die
  469.     
  470.     SetPort(fWindow);
  471.  
  472.     Rect    oldWindowRect = fWindow->portRect;
  473.     
  474.     ZoomWindow(fWindow,zoomState,false);
  475.     this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  476.  
  477.     SetPort(oldPort);
  478.     }
  479.  
  480. void
  481. TWindow::ShowHide(Boolean showFlag)
  482.     {
  483.     //    Here we need the “::” in front of ShowHide to indicate we are calling
  484.     //    the global function, and not the method ShowHide. Unintended recursion
  485.     //    can do bad things to the unsuspecting programmer.
  486.     
  487.     //    Some C++ programmers would always prepend the “::” on function calls.
  488.     
  489.     ::ShowHide(fWindow,showFlag);
  490.     fIsVisible = showFlag;
  491.     }
  492.     
  493.  
  494. Boolean
  495. TWindow::EventFilter(EventRecord * /* theEvent */)
  496.     {
  497.     return false;
  498.     }
  499.     
  500.  
  501. void
  502. TWindow::GetPerfectWindowSize(Rect *perfectSize)
  503.     {
  504.     *perfectSize = qd.screenBits.bounds;
  505.     }
  506.  
  507. void
  508. TWindow::GetWindowSizeLimits(Rect *limits)
  509.     {
  510.     limits->top = limits->left = kMinimumWindowSize;
  511.     limits->right = gDeskRectangle.right - gDeskRectangle.left;
  512.     limits->bottom = gDeskRectangle.bottom - gDeskRectangle.top;
  513.     }
  514.  
  515. void
  516. TWindow::AdjustForNewWindowSize(Rect * /* oldRect */, Rect * /* newSize */)
  517.     {
  518.     }
  519.  
  520.  
  521. Boolean
  522. TWindow::IsVisible(void)
  523.     {
  524.     return fIsVisible;
  525.     }
  526.  
  527.  
  528. Boolean
  529. TWindow::CanClose(void)
  530.     {
  531.     return true;
  532.     }
  533.  
  534.  
  535. Boolean
  536. TWindow::Close(void)
  537.     {
  538.     WindowPtr    newFrontWindow = nil;
  539.     
  540.     if (MyFrontNonFloatingWindow() == fWindow)
  541.         newFrontWindow = (WindowPtr) ((WindowPeek) fWindow)->nextWindow;
  542.  
  543.     this->Activate(false);
  544.     DisposeWindow(fWindow);
  545.  
  546.     if (newFrontWindow)
  547.         HiliteAndActivateWindow(newFrontWindow,true);
  548.  
  549.     return true;
  550.     }
  551.  
  552.  
  553. Boolean
  554. TWindow::DeleteAfterClose(void)
  555.     {
  556.     return true;
  557.     }
  558.  
  559.  
  560. void
  561. TWindow::AdjustMenusBeforeMenuSelection(void)
  562.     {
  563.     }
  564.  
  565.     
  566. void
  567. TWindow::DoMenuSelection(short /* menu */, short /* item */)
  568.     {
  569.     }
  570.     
  571.  
  572. void
  573. TWindow::DoMenuCommand(unsigned long /* menuCommand */)
  574.     {
  575.     }
  576.  
  577.  
  578. OSErr
  579. TWindow::HandleDrag(DragTrackingMessage dragMessage,DragReference theDrag)
  580.     {
  581.     OSErr    result = dragNotAcceptedErr;
  582.     
  583.     switch (dragMessage)
  584.         {
  585.         case    kDragTrackingEnterWindow:
  586.             result = this->DragEnterWindow(theDrag);
  587.             break;
  588.         
  589.         case    kDragTrackingInWindow:
  590.             result = this->DragInWindow(theDrag);
  591.             break;
  592.             
  593.         case    kDragTrackingLeaveWindow:
  594.             result = this->DragLeaveWindow(theDrag);
  595.             break;
  596.             
  597.         default:
  598.             break;
  599.         }
  600.  
  601.     return result;
  602.     }
  603.  
  604.  
  605. OSErr
  606. TWindow::DragEnterWindow(DragReference /* theDrag */)
  607.     {
  608.     return dragNotAcceptedErr;
  609.     }
  610.  
  611.  
  612. OSErr
  613. TWindow::DragInWindow(DragReference /* theDrag */)
  614.     {
  615.     return dragNotAcceptedErr;
  616.     }
  617.  
  618.  
  619. OSErr
  620. TWindow::DragLeaveWindow(DragReference /* theDrag */)
  621.     {
  622.     return dragNotAcceptedErr;
  623.     }
  624.     
  625.  
  626. OSErr
  627. TWindow::HandleDrop(DragReference /* theDrag */)
  628.     {
  629.     return dragNotAcceptedErr;
  630.     }
  631.  
  632.  
  633. ///////////////////////////////////////////////////////////////////////////
  634. //
  635. //    Utility Functions used for floating windows
  636. //
  637.  
  638. TWindow *
  639. GetWindowObject(WindowPtr aWindow)
  640.     {
  641.     short    wKind;
  642.     
  643.     if (aWindow != nil)
  644.         {
  645.         wKind = ((WindowPeek) aWindow)->windowKind;
  646.  
  647.         if (wKind >= userKind)
  648.             {
  649.             //    All windowKinds >= userKind are based upon TWindow
  650.  
  651.             return (TWindow *) GetWRefCon(aWindow);
  652.             }
  653.         }
  654.     return (TWindow *) nil;
  655.     }
  656.  
  657.  
  658. ////////////////////////////////////////////////////////////////////////
  659. //
  660. //    Utility functions
  661.  
  662.  
  663. pascal WindowPtr
  664. GetNewColorOrBlackAndWhiteWindow(short windowID, void *wStorage, WindowPtr behind)
  665.     {
  666.     if (gHasColorQuickdraw)
  667.         return GetNewCWindow(windowID,wStorage,behind);
  668.     else
  669.         return GetNewWindow(windowID,wStorage,behind);
  670.     }
  671.  
  672.  
  673. pascal WindowPtr
  674. NewColorOrBlackAndWhiteWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title, Boolean visible, short theProc, WindowPtr behind, Boolean goAwayFlag, long refCon)
  675.     {
  676.     if (gHasColorQuickdraw)
  677.         return NewCWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  678.     else
  679.         return NewWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  680.     }
  681.  
  682.  
  683. void
  684. DrawJustTheGrowIcon(WindowPtr aWindow)
  685.     {
  686.     GrafPtr        savedPort;
  687.     RgnHandle    savedClip = NewRgn();
  688.     Rect        growBoxRect;
  689.     
  690.     GetPort(&savedPort);
  691.     SetPort(aWindow);
  692.     GetClip(savedClip);
  693.  
  694.     //    clip to just the bottom right corner of the window
  695.     
  696.     growBoxRect.top = aWindow->portRect.bottom - kScrollbarWidth;
  697.     growBoxRect.bottom = aWindow->portRect.bottom;
  698.     growBoxRect.left = aWindow->portRect.right - kScrollbarWidth;
  699.     growBoxRect.right = aWindow->portRect.right;
  700.     ClipRect(&growBoxRect);
  701.  
  702.     DrawGrowIcon(aWindow);
  703.  
  704.     SetClip(savedClip);
  705.     DisposeRgn(savedClip);    
  706.  
  707.     SetPort(savedPort);
  708.     }
  709.     
  710.  
  711. WindowPtr
  712. LastFloatingWindow(void)
  713.     {
  714.     WindowPeek    aWindow = (WindowPeek) FrontWindow();
  715.     WindowPtr    lastFloater = (WindowPtr) kNoFloatingWindows;
  716.     
  717.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  718.         {
  719.         if (aWindow->visible)
  720.             lastFloater = (WindowPtr) aWindow;
  721.  
  722.         aWindow = (WindowPeek) aWindow->nextWindow;
  723.         }
  724.     return(lastFloater);
  725.     }
  726.  
  727.  
  728. WindowPtr
  729. MyFrontNonFloatingWindow(void)
  730.     {
  731.     WindowPeek    aWindow = (WindowPeek) LMGetWindowList();
  732.  
  733.     //    Skip over floating windows
  734.         
  735.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  736.         aWindow = (WindowPeek) aWindow->nextWindow;
  737.  
  738.     //    Skip over invisible, but otherwise normal windows
  739.     
  740.     while (aWindow && (aWindow->visible == 0))
  741.         aWindow = (WindowPeek) aWindow->nextWindow;
  742.         
  743.     return (WindowPtr) aWindow;
  744.     }
  745.  
  746.  
  747. void
  748. HiliteAndActivateWindow(WindowPtr aWindow,Boolean active)
  749.     {
  750.     GrafPtr        oldPort;
  751.     TWindow    *    wobj = GetWindowObject(aWindow);
  752.     
  753.     if (aWindow)
  754.         {
  755.         HiliteWindow(aWindow,active);
  756.  
  757.         if (wobj != nil)
  758.             {
  759.             GetPort(&oldPort);
  760.             SetPort(aWindow);
  761.             wobj->Activate(active);
  762.             SetPort(oldPort);
  763.             }    
  764.         }
  765.     }
  766.  
  767. void
  768. SuspendResumeWindows(Boolean resuming)
  769.     {
  770.     //    When we suspend/resume, hide/show all the visible floaters
  771.     
  772.     HiliteShowHideFloatingWindows(resuming,true);
  773.     }
  774.  
  775. void
  776. HiliteWindowsForModalDialog(Boolean hiliting)
  777.     {
  778.     //    When we display a modal dialog, we need to unhighlight
  779.     //    all visible floaters. We also need to re-hilite them
  780.     //    afterwards.
  781.     
  782.     HiliteShowHideFloatingWindows(hiliting,false);
  783.     }
  784.  
  785. void
  786. HiliteShowHideFloatingWindows(Boolean hiliting,Boolean dohiding)
  787.     {
  788.     WindowPeek    aWindow;
  789.     TWindow *    wobj;
  790.     
  791.     HiliteAndActivateWindow(MyFrontNonFloatingWindow(),hiliting);
  792.  
  793.     aWindow = (WindowPeek) LMGetWindowList();
  794.     while (aWindow && aWindow->windowKind == kFloatingWindowKind)
  795.         {
  796.         wobj = GetWindowObject((WindowPtr) aWindow);
  797.         
  798.         //    If we are hiding or showing, only hide/show windows
  799.         //    that were visible to begin with.
  800.         
  801.         //    NOTE:    We use our copy of the visible flag so we can
  802.         //            automatically show floaters on a resume event.
  803.         
  804.         //    NOTE:    Since this isn’t a method of TWindow, we don’t
  805.         //            really need the “::” on ShowHide, but as long
  806.         //            as we’re trying to avoid ambiguity.
  807.         
  808.         if (dohiding && (wobj != nil) && (wobj->IsVisible()))
  809.             ::ShowHide((WindowPtr) aWindow,hiliting);
  810.             
  811.         //    All floaters are hilited if any floater is hilited
  812.  
  813.         HiliteWindow((WindowPtr) aWindow,hiliting);
  814.         aWindow = (WindowPeek) aWindow->nextWindow;
  815.         }
  816.     }
  817.  
  818.  
  819. ///////////////////////////////////////////////////////////////////////////
  820. //
  821. //    Routines used for dealing with windows and multiple screens
  822. //
  823.  
  824. pascal void
  825. CalculateWindowAreaOnDevice(short /* depth */,short /* deviceFlags */,GDHandle targetDevice,long userData)
  826.     {
  827.     CalcWindowAreaDeviceLoopUserData *    deviceLoopDataPtr;
  828.     long                                windowAreaOnThisScreen;
  829.     Rect                                windowRectOnThisScreen;
  830.     
  831.     deviceLoopDataPtr = (CalcWindowAreaDeviceLoopUserData *) userData;
  832.  
  833.     SectRect(&deviceLoopDataPtr->fWindowBounds, &(**targetDevice).gdRect,&windowRectOnThisScreen);
  834.     OffsetRect(&windowRectOnThisScreen,-windowRectOnThisScreen.left,-windowRectOnThisScreen.top);
  835.     windowAreaOnThisScreen = windowRectOnThisScreen.right * windowRectOnThisScreen.bottom;
  836.  
  837.     if (windowAreaOnThisScreen > deviceLoopDataPtr->fLargestArea)
  838.         {
  839.         deviceLoopDataPtr->fLargestArea = windowAreaOnThisScreen;
  840.         deviceLoopDataPtr->fScreenWithLargestPartOfWindow = targetDevice;
  841.         }
  842.     }
  843.  
  844.  
  845. DeviceLoopDrawingUPP CallCalcWindowAreaOnDevice = NewDeviceLoopDrawingProc(&CalculateWindowAreaOnDevice);
  846.  
  847.  
  848. void
  849. FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect,GDHandle * theBestDevice)
  850.     {
  851.     RgnHandle                            copyOfWindowStrucRgn;
  852.     CalcWindowAreaDeviceLoopUserData    deviceLoopData;
  853.  
  854.     //    Use DeviceLoop to find out what GDevice contains the largest
  855.     //    portion of the supplied window.
  856.     //
  857.     //    NOTE:    Assumes thePort == the Window Manager Port because we using
  858.     //            the window strucRgn, not contRgn.
  859.  
  860.     deviceLoopData.fScreenWithLargestPartOfWindow = nil;
  861.     deviceLoopData.fLargestArea = -1;
  862.     deviceLoopData.fWindowBounds = (**(((WindowPeek) aWindow)->contRgn)).rgnBBox;
  863.     
  864.     copyOfWindowStrucRgn = NewRgn();
  865.     CopyRgn(((WindowPeek) aWindow)->strucRgn,copyOfWindowStrucRgn);
  866.  
  867.     DeviceLoop(copyOfWindowStrucRgn,CallCalcWindowAreaOnDevice,(long) &deviceLoopData,singleDevices);    
  868.  
  869.     DisposeRgn(copyOfWindowStrucRgn);
  870.     
  871.     *theBestDevice = deviceLoopData.fScreenWithLargestPartOfWindow;
  872.     *theBestScreenRect = (**(deviceLoopData.fScreenWithLargestPartOfWindow)).gdRect;
  873.  
  874.     //    Leave some space around the edges of the screen so window look good, AND
  875.     //    if the best device is the main screen, leave space for the Menubar
  876.     
  877.     InsetRect(theBestScreenRect,kScreenEdgeSlop,kScreenEdgeSlop);
  878.     if (GetMainDevice() == deviceLoopData.fScreenWithLargestPartOfWindow)
  879.         theBestScreenRect->top += GetMBarHeight();
  880.     }
  881.  
  882.  
  883. ///////////////////////////////////////////////////////////////////////////
  884. //
  885. //    Drag Manager callback routines which dispatch to a window’s method
  886. //
  887.  
  888. pascal OSErr
  889. CallWindowDragTrackingHandler(DragTrackingMessage dragMessage,WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  890.     {
  891.     TWindow *wobj = GetWindowObject(theWindow);
  892.     
  893.     if (wobj)
  894.         return(wobj->HandleDrag(dragMessage,theDrag));
  895.     else
  896.         return dragNotAcceptedErr;
  897.     }
  898.  
  899.     
  900. pascal OSErr
  901. CallWindowDragReceiveHandler(WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  902.     {
  903.     TWindow *wobj = GetWindowObject(theWindow);
  904.     
  905.     if (wobj)
  906.         return(wobj->HandleDrop(theDrag));
  907.     else
  908.         return dragNotAcceptedErr;
  909.     }
  910.